const { get } = require("../../until/db.js");

// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
    hotList:[],
    history:[]
  },
  async onLoad(){
    var result = await get("menu",{},0,10,{field:'views',sort:'desc'})
    // console.log(result);
     var hotList = result.data
     this.setData({
       hotList
     })
  },
  onShow(){
    var history = wx.getStorageSync("history")||[];
    // console.log(history)
    this.setData({
      history
    })
  },
  myinput(e){
    // console.log(e);
    var keyword = e.detail.value;
    this.data.keyword = keyword;
  },
  dosearch(){
    var arr = wx.getStorageSync("history")||[];
    var index = arr.findIndex(item=>{
      return item == this.data.keyword
    })
    if(index==-1){
      arr.unshift(this.data.keyword) 
    }else{
      arr.splice(index,1)
      arr.unshift(this.data.keyword)
    }
    wx.setStorageSync("history",arr)
   

    wx.navigateTo({ 
      url: '/pages/searchList/searchList?keyword='+this.data.keyword
    })
  },
  toSlist(e){
    // console.log(e);
    var keyword = e.currentTarget.dataset.keyword
    // console.log(keyword);
    wx.navigateTo({
      url: '/pages/searchList/searchList?keyword='+keyword,
    })
  },
  toDetail(e){
    var id = e.currentTarget.id
    var name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/detail/detail?id='+id+'&name='+name,
    })
  }
})